package com.meixiang.dialog;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.tool.Tool;
import com.meixiang.view.WheelView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * 多项选择dialog
 *
 * author:xiejingwen
 */
public class TitleSelectDialog extends Dialog {
    private IResultListener listener;
    private LinearLayout ll_layout;
    private TextView mTitleTextView;
    private TextView right_title;
    private String title;
    private String rightTitle;
    private Context context;
    private View view;
    private List<String> data = new ArrayList<>();
    private WheelView listView;
    private  int mSelectedIndex;
    private String mItem="";
    private int defualtItemPosition=-1;

    /**
     * 无右边title按钮构造
     * @param context 上下文对象
     * @param title   标题
     * @param defualtItem 默认下标
     * @param data  数据
     * @param listener 回调
     */
    public TitleSelectDialog(Context context, String title,String defualtItem,String[] data, IResultListener listener) {
        super(context);
        this.context=context;
        this.listener=listener;
        this.title=title;
        for (int i=0;i<data.length;i++){
            this.data.add(data[i]);
            if (data[i].equals(defualtItem)){
                defualtItemPosition=i;
            }
        }
        show();
    }

    /**
     * 有右边title按钮构造
     * @param context 上下文对象
     * @param title   标题
     * @param rightTitle 右边标题
     * @param defualtItem 默认下标
     * @param data  数据
     * @param listener 回调
     */
    public TitleSelectDialog(Context context, String title,String rightTitle,String defualtItem,String[] data, IResultListener listener) {
        super(context);
        this.context=context;
        this.listener=listener;
        this.title=title;
        this.rightTitle=rightTitle;
        for (int i=0;i<data.length;i++){
            this.data.add(data[i]);
            if (data[i].equals(defualtItem)){
                defualtItemPosition=i;
            }
        }
        show();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        view= LayoutInflater.from(context).inflate(R.layout.dialog_title_select,null);
        setContentView(view);
        initDialog();
        initView();

    }
    private void initDialog(){
        setCancelable(false);
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        int width= Tool.getWindowWith(context);
        WindowManager.LayoutParams params=getWindow().getAttributes();
        params.width=width;
        params.height= ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
    }
    private void initView(){
        listView = ButterKnife.findById(this, R.id.select_dialog_list);
        listView.setOffset(2);
        listView.setItems(data);
        if (defualtItemPosition!=-1) {
            listView.setSeletion(defualtItemPosition);
        }
        listView.setOnWheelViewListener(new WheelView.IWheelView() {
            @Override
            public void OnWheelViewListener(int selectedIndex, String item) {

                mSelectedIndex = selectedIndex;
                mItem = item;
            }
        });
        mTitleTextView = (TextView) findViewById(R.id.title);
        right_title= (TextView) findViewById(R.id.right_title);
        ll_layout= (LinearLayout) findViewById(R.id.ll_layout);
        mTitleTextView.setText(title);
        right_title.setText(rightTitle);
        right_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null==mItem||"".equals(mItem)){
                }else{
                    listener.RightTitleOnclick(v,mSelectedIndex,mItem);
                }
                dismiss();
            }
        });
    }

    public interface IResultListener{
        /**
         * 右title点击事件
         * @param view
         * @param selectedIndex
         * @param item
         */
        void RightTitleOnclick(View view,int selectedIndex, String item);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onStart() {
        TitleSelectDialog.super.onStart();
      /*  ViewPropertyAnimator.animate(view).translationY(view.getHeight() - ll_layout.getHeight()).setDuration(0).start();
        ViewPropertyAnimator.animate(view).translationY(0).setDuration(300).start();*/
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            dismiss();
        }
        return super.onKeyDown(keyCode, event);
    }
}
